package br.unirio.webdisco.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{  
	  
    private File upload;   
    private String uploadFileName; //nome do arquivo enviado  
    private String uploadContentType; //contexto do arquivo, imagem, txt etc...      
      
    public File getUpload() {  
        return upload;  
    }  
      
    public String moveArquivo(){  
          
        copyFile(upload, new File("c:\\uploads\\"+ uploadFileName));   
          
        return SUCCESS;  
    }  
      
    // Metodo que move o arquivo para o diretorio desejado  
  
    private static void copyFile(File in, File out) {  
          
        try {  
            FileChannel sourceChannel = new FileInputStream(in).getChannel();  
            FileChannel destinationChannel = new FileOutputStream(out).getChannel();  
            sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);  
            sourceChannel.close();  
            destinationChannel.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();   
        }  
    }  
      
      
    public void setUpload(File upload) {  
        this.upload = upload;  
    }  
  
    public String getUploadFileName() {  
        return uploadFileName;  
    }  
  
    public void setUploadFileName(String uploadFileName) {  
        this.uploadFileName = uploadFileName;  
    }  
  
    public String getUploadContentType() {  
        return uploadContentType;  
    }  
  
    public void setUploadContentType(String uploadContentType) {  
        this.uploadContentType = uploadContentType;  
    }   
  
}
